@isTest
private class ContractProductControllerTest {

    static testMethod void testMethod1() {
          //客户1
        Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');

        insert acc;     

        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;


        //客户信用
        AccountCredit__c arc =  new AccountCredit__c();
        arc.CreditLimit__c = 100;
        arc.NCCreditDeadline__c = '10';
        arc.Account__c  = acc.Id;

        arc.Company__c = '四川省迈克实业有限公司';
        insert arc ;

                //for (AccountCredit__c ac :[select CreditLimit__c,NCCreditDeadline__c,Id,Company__c,Account__c



         //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id,IsActive__c = true);
        qt2.Status = '已批准';
        insert qt2 ;

        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

        Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        insert p ;

        Product__c p1 = TestUtils.createProduct();
        p1.Name = 'Test1';
        p1.NCCode__c = '100100100001'; 
        p1.RecordTypeId = pRT;
        p1.zl__c = '10';

        insert p1;

        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'Test12';
        p2.NCCode__c = '100100100002'; 
        p2.RecordTypeId = pRT;
        p2.zl__c = '10';

        insert p2;

         Product__c p3 = TestUtils.createProduct();
        p3.Name = 'Test13';
        p3.NCCode__c = '110220600001'; 
        p3.RecordTypeId = pRT;
        p3.zl__c = '10';

        insert p3;

         Product__c p4 = TestUtils.createProduct();
        p4.Name = 'Test14';
        p4.NCCode__c = '110220600002'; 
        p4.RecordTypeId = pRT;
        p4.zl__c = '10';

        insert p4;

         IntentProduct__c ip = new IntentProduct__c();
        ip.Product__c = p1.Id;
        ip.Quantity__c = 1;
        ip.SalesPrice__c = 1;
        ip.Opportunity__c = opp.Id;
        insert ip;


           //血糖仪申请

        BloodSugarOrder__c b1 = new BloodSugarOrder__c();
        b1.Effect__c  = true;
        b1.Account__c = acc.Id;
        b1.Opportunities__c = opp.Id;
        b1.Status__c = '审批成功';
        insert b1;

        BloodSugarOrderDetail__c bd = new BloodSugarOrderDetail__c();
        bd.BloodSugarOrder__c = b1.Id;
        bd.BloodSugarProduct__c = ip.Id;
        bd.DistributionProductNumber__c = 1;
        bd.DistributionProduct__c = p3.Id;
        insert bd ; 


         //1.创建仪器政策模板
        MarketingPolicy__c  mp = new MarketingPolicy__c();
        mp.Product__c =p.Id ;
        mp.Category1__c = '生化';
        mp.Category2__c = '生化-试剂';
        mp.AmountPurchased__c = 10000;
        mp.BloodCardPurchased__c = 12000;
        mp.Company__c = 'MKSY';
        mp.CooperationPeriod__c = 5;
        mp.EarnestMoney__c = 10000;
        mp.PolicyType__c = '租赁合作政策';

        insert mp ; 

        InstrumentPolicy__c ipo = new InstrumentPolicy__c();
        ipo.Account__c = acc.Id;
        ipo.Opportunity__c = opp.Id;
        ipo.Active__c = true;
        ipo.Status__c = '已批准';

        insert ipo;


        InstrumentMarketingPolicy__c imp = new InstrumentMarketingPolicy__c();
        imp.InstrumentPolicy__c = ipo.Id;
        imp.MarketingPolicy__c = mp.Id;
        imp.Product__c = p.Id;
        insert imp;


        update imp;






        //测算信息1
        Measure_Calculate__c mc = new Measure_Calculate__c(Opportunity__c = opp.Id);
        mc.IsActive__c = true;
        mc.Status__c = '审批成功';
        insert mc;

      

        //产品1 
        List<Product__c> plist =new List<Product__c>();

       // Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        Set<String> cates = new Set<String>{'免疫','病理','生化','血糖','血液','血型'};
        for (String cate  : cates) {
            Product__c pp = TestUtils.createProduct();
            pp.Name = 'Test'+cate;
            pp.Category1__c = cate; 
            pp.RecordTypeId = pRT;

            plist.add(pp);

         }  

        insert plist;
        //List<IntentProduct__c> ipList = new List<IntentProduct__c>();
        List<QuoteProduct__c> quoteList = new List<QuoteProduct__c>();
        List<Price__c> priceList =new List<Price__c>();
        List<Measure_Calculate_Product__c> mcList = new List<Measure_Calculate_Product__c>();

        for (Product__c Product2 :  plist) {
            Price__c price2 = TestUtils.createPrice();
            price2.Product__c = Product2.Id;
            price2.Company__c = '四川省迈克实业有限公司';
            price2.InstCost__c = 10.0;
            price2.HospitalManagerPrice__c = 10;
            price2.InvoiceDealerPrice__c = 10;
            price2.SpecialPrice__c = 10;
            price2.ProvincialAgencyPrice__c = 10;


           // ipList.add(new IntentProduct__c(Opportunity__c = opp.Id ,Product__c=p.Id));
      
            mcList.add(new Measure_Calculate_Product__c(Product__c = Product2.Id, Measure_Calculate__c = mc.Id));
            priceList.add(price2);
            quoteList.add(new QuoteProduct__c(Name = '测试报价产品2',Product__c = Product2.Id,Quote__c = qt2.Id,SalesPrice__c = 5));
        
        }

       // insert ipList;
        insert priceList;   
        insert quoteList;
        insert mcList;



        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
        con.Status__c  = '草稿';
        con.Corpcode__c = 'MKSY';
        insert con;

       

 
        

         System.Test.startTest(); 

          PageReference page2 = new PageReference('/apex/ContractProduct?id=' + acc.Id );
        System.Test.setCurrentPage(page2);
        ContractProductController cpc2 = new ContractProductController();
        cpc2.init();


        PageReference page = new PageReference('/apex/ContractProduct?id=' + con.Id );
        System.Test.setCurrentPage(page);
        ContractProductController cpc = new ContractProductController();
        cpc.init();
        Map<String,String> searchMap = new Map<String,String>();
        searchMap.put('type', 'QuoteRegent');
        cpc.searchJson = JSON.serialize(searchMap);
        cpc.getProduct();


        searchMap.put('type', 'MeasureCalc');
        cpc.searchJson = JSON.serialize(searchMap);
        cpc.getProduct();

        searchMap.put('type', 'Blood');
        cpc.searchJson = JSON.serialize(searchMap);
        cpc.getProduct();

        searchMap.put('type', 'Policy');
        cpc.searchJson = JSON.serialize(searchMap);
        cpc.getProduct();
        cpc.getCoopProduct();

        List<ContractProductController.CProductBean> cpList = new List<ContractProductController.CProductBean>();
        cpList.add(new ContractProductController.CProductBean(imp));

        for (QuoteProduct__c qp : quoteList ) {
            ContractProductController.CProductBean cpb = new ContractProductController.CProductBean(qp,false);
            cpList.add(cpb);
        }
        Map<String,Object> saveMap = new Map<String,Object>();
        saveMap.put('data', cpList);
        saveMap.put('account', '');
        cpc.saveJson  = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');
        //cpc.save();
        cpc.newSave();

        Map<String,String> baseMap = new Map<String,String>();
        baseMap.put('ApproveType__c', '1');

        saveMap.put('base', baseMap);

        cpc.saveJson  = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');
        //cpc.save();
       
        cpc.newSave();

        cpc.saveContract();

        cpc.init();


        
         PageReference page1 = new PageReference('/apex/ContractProduct?id=' + opp.Id );
        System.Test.setCurrentPage(page1);
        ContractProductController cpc1 = new ContractProductController();
        cpc1.init();


      


        try {
           con.Status__c = '已生效';
            update con ;  
        }
        catch (Exception e) {
            
        }







        PageReference pagec = new PageReference('/apex/ContractProductPDF?conid=' + con.Id+'&type=2' );
        System.Test.setCurrentPage(pagec);
        ContractProductPDFController cpp = new ContractProductPDFController();







    }
}